
var hosDao = require('../dao/hospital.dao.js');
var userDao = require('../dao/user.dao.js');


module.exports.create = function(req, res) {
  //console.log('req.body='+JSON.stringify(req.body));
  var dataPkg = req.body;
  //console.log('dataPkg='+dataPkg);
  hosDao.createHospital(dataPkg.model,function(err,hospital){
    if (err) {
      console.log(err);
      res.status(406).json(err);
      return;
    }
    var userInfo = dataPkg.userInfo;
    //console.log('userInfo',JSON.stringify(userInfo,null,2));
    if(userInfo && userInfo.role!='root' && hospital!=null){
      //fill hid to user here
      //*
      userDao.appendHos2User(hospital._id,userInfo.uid,function(err,user){
        if(err){
          console.error(err);
          return;
        }
        //console.log('appendHos2User ok');
      });//*/
    }
    res.status(201).json(hospital);
  });
};

module.exports.showList = function(req, res) {
  var filter = null;
  //console.log('req.body='+JSON.stringify(req.body,null,2));
  var userInfo = req.body;
  //console.log('userInfo='+JSON.stringify(userInfo,null,2));
  if(userInfo && userInfo.role != 'root'){
    //for not root user,filter hospital.
    userDao.getUserHosList(userInfo.uid,function(err,hosList){
      if(err){
        console.error(err);
        res.status(400).json(err);
      }
      //console.log('hosList=',JSON.stringify(hosList,null,2));
      filter = {_id:{$in:hosList},status:1};
      hosDao.getHospitalList(filter,20,function(err,rows){
        if (err) {
          console.log(err);
          res.status(400).json(err);
        }
        res.status(200).json(rows);
      });
    });
    return;
  }
  //console.log('root user get all list.');
  hosDao.getHospitalList(filter,20,function(err,rows){
    //console.log('rows.length=',rows.length);
    if (err) {
      console.log(err);
      res.status(400).json(err);
    }
    res.status(200).json(rows);
  });
};

module.exports.updateOne = function(req, res) {
  //console.log('api controllers===req.params.hid='+req.params.hid);
  hosDao.updateHospital(req.params.hid,req.body,function(err,updateRes){
    if (err) {
      console.log(err);
      res.status(406).json('update failed:'+err);
    }
    //console.log('updateRes',JSON.stringify(updateRes,null,2));
    res.status(200).json(updateRes);
  });
};

module.exports.deleteOne = function(req, res) {
  //console.log('api controllers===req.params.hid='+req.params.hid);
  var hid = req.params.hid;
  hosDao.removeHospital(hid,function(err,updatedHospital){
    if (err) {
      console.log(err);
      //res.status(404).json(err);
      res.status(500).json('hospital remove failed.'+err);
    }
    res.status(200).json(updatedHospital);
  });
};

module.exports.readOne = function(req, res) {
  //console.log('api controllers.readOne===req.params.hid='+req.params.hid);
  var hid = req.params.hid;
  hosDao.getHospital(hid,function(err,hospital){
    if (err) {
      console.log(err);
      res.status(404).json(err);
    }
    //console.log(JSON.stringify(hospital,null,2));
    res.status(200).json(hospital);
  });
};

